<template>
  <div class="batch-detail-container">
    <el-container class="layout-container">
      <el-aside width="220px" class="sidebar">
        <div class="sidebar-content">
          <el-button @click="goBack" class="back-btn">
            <el-icon><ArrowLeft /></el-icon>
            返回列表
          </el-button>
        </div>
      </el-aside>

      <el-main class="main-content">
        <div class="page-header">
          <h1>产品批号详情</h1>
          <p>查看产品批号详细信息</p>
        </div>

        <el-card class="detail-card" shadow="hover" v-loading="loading">
          <template #header>
            <div class="card-header">
              <el-icon><View /></el-icon>
              <span>批号信息</span>
            </div>
          </template>

          <div v-if="Object.keys(batchDetail).length > 0" class="detail-content">
            <el-descriptions :column="1" border>
              <el-descriptions-item label="产品批号">
                {{ batchDetail.batchId }}
              </el-descriptions-item>
              <el-descriptions-item label="产品类型">
                {{ batchDetail.productType }}
              </el-descriptions-item>
              <el-descriptions-item label="品种">
                {{ batchDetail.productVariety }}
              </el-descriptions-item>
              <el-descriptions-item label="屠宰企业所在省">
                {{ batchDetail.provName }}
              </el-descriptions-item>
              <el-descriptions-item label="屠宰企业所在市">
                {{ batchDetail.cityName }}
              </el-descriptions-item>
              <el-descriptions-item label="屠宰企业名称">
                {{ batchDetail.slauEntName }}
              </el-descriptions-item>
              <el-descriptions-item label="屠宰企业产品批号">
                {{ batchDetail.slauBatchId }}
              </el-descriptions-item>
              <el-descriptions-item label="状态">
                <el-tag :type="formatStateTag(batchDetail.state)">
                  {{ formatState(batchDetail.state) }}
                </el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="创建时间">
                {{ batchDetail.createTime }}
              </el-descriptions-item>
            </el-descriptions>
          </div>

          <div v-else class="empty-container">
            <el-empty description="暂无数据" />
          </div>
        </el-card>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowLeft, View } from '@element-plus/icons-vue'
import { getWholBatchDetailService } from '@/api/wholesaler'

const router = useRouter()
const route = useRoute()
const wbId = route.params.id
const batchDetail = ref({})
const loading = ref(false)

// 格式化状态
const formatState = (state) => {
  const stateMap = {
    1: '新建',
    2: '待确认',
    3: '已确认',
    4: '已下架'
  }
  return stateMap[state] || '未知'
}

// 格式化状态标签类型
const formatStateTag = (state) => {
  const typeMap = {
    1: 'primary',
    2: 'warning',
    3: 'success',
    4: 'info'
  }
  return typeMap[state] || 'info'
}

// 返回列表
const goBack = () => {
  router.push('/ent/farm')
}

// 加载详情数据
const loadDetail = async () => {
  if (!wbId) {
    ElMessage.error('无效的批号ID')
    return
  }
  
  loading.value = true
  try {
    const res = await getWholBatchDetailService(wbId)
    batchDetail.value = res.data || {}
  } catch (error) {
    ElMessage.error('加载详情失败: ' + (error.message || '未知错误'))
    batchDetail.value = {}
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadDetail()
})
</script>

<style scoped>
.batch-detail-container {
  height: 100vh;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
}

.layout-container {
  height: 100%;
}

.sidebar {
  background: linear-gradient(180deg, #2d3748 0%, #1a202c 100%);
  padding: 20px;
  display: flex;
  align-items: flex-start;
}

.sidebar-content {
  width: 100%;
}

.back-btn {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: white;
  transition: all 0.3s ease;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
}

.back-btn .el-icon {
  margin-right: 8px;
}

.main-content {
  padding: 30px;
  background: transparent;
  overflow-y: auto;
}

.page-header {
  margin-bottom: 30px;
}

.page-header h1 {
  margin: 0 0 8px 0;
  color: #2d3748;
  font-size: 28px;
  font-weight: 600;
}

.page-header p {
  margin: 0;
  color: #718096;
  font-size: 16px;
}

.detail-card {
  max-width: 800px;
  border-radius: 12px;
  border: none;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  background: rgba(255, 255, 255, 0.95);
}

.card-header {
  display: flex;
  align-items: center;
  font-size: 18px;
  font-weight: 600;
  color: #2d3748;
}

.card-header .el-icon {
  margin-right: 8px;
  color: #409eff;
}

.detail-content {
  padding: 20px 0;
}

.empty-container {
  padding: 60px 20px;
  display: flex;
  justify-content: center;
}

@media (max-width: 768px) {
  .sidebar {
    width: 100% !important;
    height: auto;
  }

  .main-content {
    padding: 20px;
  }

  .page-header h1 {
    font-size: 24px;
  }
}
</style>